//
//  VisitorLoginView.swift
//  SinaWeiBo
//
//  Created by tuyue on 16/3/6.
//  Copyright © 2016年 xueyang.co.ltd. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    optional func visitorLoginViewDidRegister();
    optional func visitorLoginViewDidLogin();
    optional func visitorLoginViewDidgoLook()
}

class VisitorLoginView: UIView {
    
   weak var delegate: VisitorLoginViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
//        print(frame)
        setUI()
        
    }
    
    //一旦重写了 init(frame方法之后 系统会默认该视图会通过手写代码创建
    //系统就不会再让同构xib加载视图
    //通过xib加载的UIView及其子类会调用这个方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置转盘动画
    /**设置转盘动画*/
    func animationAboutCircleIcon(){
        /**创建一个CABasicAnimation动画对象，设置为旋转动画*/
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        /**设置动画的重复次数为最大次数*/
        animation.repeatCount = MAXFLOAT
        /**设置动画的旋转角度*/
        animation.toValue = 2 * M_PI
        /**设置动画的持续时间*/
        animation.duration = 20
        /**将动画添加到circleIcon的layer中*/
        animation.removedOnCompletion = false
        circleIcon.layer.addAnimation(animation, forKey: nil)
    }
    
    //MARK: 登录界面的判断，向外面提供的方法
    /**如果为空imageName，就跳转到home界面*/
    func setupInfoWith(imageName: String? , lbText: String ){
        if imageName != nil {
        largeHouse.image = UIImage(named: imageName!)
//      circleIcon.layer.removeAllAnimations()
        tipLabel.text = lbText
        circleIcon.hidden = true
        backImage.hidden = true
            
        }else{
           animationAboutCircleIcon()
           tipLabel.text = lbText
            registerBtn.hidden = true
            loginBtn.hidden = true
            addSubview(goLookBtn)
            //MARK: - loginBtn的点击事件
            
            goLookBtn.translatesAutoresizingMaskIntoConstraints = false
            goLookBtn.snp_makeConstraints(closure: { (make) -> Void in
                make.centerX.equalTo(tipLabel.snp_centerX)
                make.top.equalTo(tipLabel.snp_bottom).offset(16)
                make.width.equalTo(150)
                
                goLookBtn.addTarget(self, action: "goLook", forControlEvents: .TouchUpInside)
                
                
            })
        }
    }
    
    //MARK: - 设置按钮的点击事件
    @objc private func login(){
        delegate?.visitorLoginViewDidLogin?()
    }
    
    @objc private func register(){
        delegate?.visitorLoginViewDidRegister?()
    }
    
    @objc private func goLook(){
        delegate?.visitorLoginViewDidgoLook?()
    }

    
    
    //MARK: - 设置UI的约束的方法
    func setUI(){
//        animationAboutCircleIcon()
        /**添加圆环图片，并设置frame*/
        addSubview(circleIcon)
        /**手写代码需要手动设置一个属性  屏蔽frame布局的方式*/
        circleIcon.translatesAutoresizingMaskIntoConstraints = false
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        /**添加圆环和房子间的渐变样式的背景图片，并设置frame*/
        addSubview(backImage)
        backImage.translatesAutoresizingMaskIntoConstraints = false
        backImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
            
        }
        
        
        /**添加房子图片并设置frame*/
        addSubview(largeHouse)
        largeHouse.translatesAutoresizingMaskIntoConstraints = false
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.centerY.equalTo(backImage.snp_centerY).offset(10)
            
        }
        
        
        /**添加提示文案并设置frame*/
        addSubview(tipLabel)
        tipLabel.translatesAutoresizingMaskIntoConstraints = false
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            /**固定label的宽高*/
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        
        /**添加登陆按钮并设置frame*/
        addSubview(loginBtn)
        //MARK: - loginBtn的点击事件
        loginBtn.addTarget(self, action: "login", forControlEvents: .TouchUpInside)
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
//            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        
        /**添加注册按钮并设置frame*/
        addSubview(registerBtn)
        //MARK: - registerBtn的点击事件
        registerBtn.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
        
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
//            make.width.equalTo(100)
            make.width.equalTo(loginBtn.snp_width)
            make.leftMargin.equalTo(loginBtn.snp_right).offset(30)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
    }
    
    
    //MARK: - 创建访客界面的子控件
    /**懒加载创建并设置转盘图片*/
    private lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /**懒加载创建并设置渐变背景图片*/
    private lazy var backImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /**懒加载创建并设置大房子图片*/
    private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    /**懒加载创建并设置提示Label*/
    private lazy var tipLabel:UILabel = UIFactory.factoryLable("", fontSize: 14, textColor: UIColor.darkGrayColor() )

    
    
    /**懒加载创建登陆按钮及设置*/
    
    private lazy var loginBtn:UIButton = UIFactory.factoryButtonWithTitle("登录", backImageName: "common_button_white_disable", titleColor: UIColor.orangeColor(), fontSize: 16)

    
    
    /**懒加载创建注册按钮及设置*/
    private lazy var registerBtn:UIButton = UIFactory.factoryButtonWithTitle("注册", backImageName: "common_button_white_disable", titleColor: UIColor.orangeColor(), fontSize: 16)

    
    /**懒加载创建设置去关注按钮*/
    private lazy var goLookBtn:UIButton = UIFactory.factoryButtonWithTitle("去关注", backImageName: "common_button_white_disable", titleColor: UIColor.orangeColor(), fontSize: 16)
    
}
